jQuery(document).ready(function(){
	jQuery(window).scroll(scrollEvent);
	
	function bindCommentAction(){
		bindShowComment();
		bindAddComment();
		
		jQuery("div#comment div.comment_list").hide();
		
		bindDeleteCommentButton();
		
		jQuery("textarea.your_comment").focus(function(){
			var $input = jQuery(this);
			var value = $input.val();
			if(value == 'Ý kiến của bạn....')
				$input.val('');
		}).blur(function(){
			var $input = jQuery(this);
			var value = $input.val();
			if(jQuery.trim(value) == '')
				$input.val('Ý kiến của bạn....');
		});
	}
	
	function bindEditEntryAction(){
		jQuery(".edit_entry_pop").unbind('click').click(function() {
			// bind edit button for goal entry
			entryId = jQuery(this).attr("rel");
			showEditor(entryId);
		});
	}
	
	function scrollEvent(){
		var $mileStone = jQuery("div#next_page_scroll");		
		if($mileStone.length == 0){
			return;
		}
		
		var _url = getNextURL($mileStone);
	    
	    var docViewTop = jQuery(window).scrollTop();
	    var docViewBottom = docViewTop + jQuery(window).height();

	    var elemTop = $mileStone.offset().top;
	    var elemBottom = elemTop + $mileStone.height();	
	    
	    if((elemBottom >= docViewTop) && (elemTop <= docViewBottom)){
	    	jQuery(window).unbind('scroll');
	    	$mileStone.remove();
	    	
	    	jQuery.ajax({
		    	url: _url,
			    success: function(data) {	    		
	    			data = jQuery.trim(data);
	    			if(data == ""){
	    				jQuery("#scroll_loader").hide();
	    				return;
	    			}
	    			
	    			jQuery("#scroll_loader").hide().remove();
	    			var $div = jQuery("<div/>").append(data);
	    			$div.hide();
		    		jQuery("div#entry_container").append($div);
		    		$div.fadeIn("slow");
		    		bindEditEntryAction();		    	
		    		bindCommentAction();
		    		bindLikeAction();
		    		applyReadmore();
		    		jQuery(".ppy").popeye();
		    		bindNextPageButton();
			    },
			    beforeSend: function(){
			    	jQuery("#scroll_loader").show();
			    }
		    });
	    }	    
	}
	
	function bindNextPageButton(){
		var $mileStone = jQuery("div#next_page_scroll");		
		if($mileStone.length == 0){
			return;
		}
		var _url = getNextURL($mileStone);
		
		jQuery("a#show_more_button").click(function(){
			$mileStone.remove();
	    	
	    	jQuery.ajax({
		    	url: _url,
			    success: function(data) {
	    			jQuery(this).remove();
	    			data = jQuery.trim(data);
	    			if(data == ""){
	    				jQuery("#scroll_loader").hide();
	    				return;
	    			}
	    			
	    			jQuery("#scroll_loader").hide().remove();
	    			jQuery("#next_page_scroll").remove();
	    			jQuery("#show_more").remove();
	    			var $div = jQuery("<div/>").append(data);
	    			$div.hide();
		    		jQuery("div#entry_container").append($div);
		    		$div.fadeIn("slow");
		    		bindEditEntryAction();		    	
		    		bindCommentAction();
		    		bindLikeAction();
		    		applyReadmore();
		    		jQuery(".ppy").popeye();
		    		bindNextPageButton();
			    },
			    beforeSend: function(){
			    	jQuery("#scroll_loader").show();
			    }
		    });
	    	
			return false;
		});
	}
	
	function getNextURL($mileStone){
		var lastValue = $mileStone.attr("rel");
		
	    var urlData = $mileStone.attr("class");
	    urlData = urlData.split(";");
	    
	    var view=urlData[0];
	    var userId=urlData[1];
	    var goalId=urlData[2];
	    var _url=ctx+"/ajax/"+view+".html?userId="+userId+"&goalId="+goalId+"&lastOrderValue="+lastValue;
	    return _url;
	}
	
	function bindScroll(){
		jQuery(window).scroll(scrollEvent);
	}
});